<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>钱包盒子-用户登录</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="../Public/css/Home/mui.css" />
		<link rel="stylesheet" href="../Public/css/Home/public.css" />
		<link rel="stylesheet" href="../Public/css/Home/login.css" />
	</head>

	<body class="bg-white">
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">用户登录</h1>
		</header>
		<div class="mui-content bg-white">
			<form class="login">
				<div class="user-img"><img src="../Public/img/img-user.png" /></div>
				<ul>
					<li class="layout-center">
						<label class="area-box">
	                        <span class="area_code">+ 86</span>
	                        <i class="mui-icon mui-icon-arrowdown"></i>
	                    </label>
						<div class="flex-item1">
							<input id="userName" type="tel" placeholder="手机号" />
						</div>
					</li>
					<li class="layout-center">
						<div class="flex-item1">
							<input type="number" name="checkk" id="code" placeholder="验证码">
						</div>
						<span id="getCode" class="btn-vcode">获取验证码</span>
					</li>
					<li class="layout-center">
						<div class="flex-item1">
							<input id="password" type="password" placeholder="密码(6-18位)" />
						</div>
					</li>
				</ul>
				<div class="btn-box">
					<a href="javascript:void(0);" id="loginBtn" class="btn btn_login">登录</a>
					<a href="javascript:void(0);" id="register" class="btn btn_register">注册</a>
				</div>
			</form>
		</div>
	</body>
	<script src="../Public/js/Home/jquery.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../Public/js/Home/mui.js" type="text/javascript" charset="utf-8"></script>
	<script src="../Public/js/Home/common.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		mui.init();
		
		var trueCode;
		var getCodeBtn = document.getElementById("getCode");
		var mobilePhone = document.getElementById('userName');
		
		getCodeBtn.addEventListener('tap', getCode);
		
		document.getElementById("loginBtn").addEventListener('tap', function() {
			var user_name = mobilePhone.value;
			var user_pwd = document.getElementById("password").value;
			var identify = document.getElementById("code").value;
			
			if(!user_name) {
				common.alert("提示", "请输入手机号!");
				return false;
			}
			if(!user_pwd) {
				common.alert("提示", "请输入密码!");
				return false;
			}
			if(!identify) {
				common.alert("提示", "请输入验证码!");
				return false;
			}
			var params = {
				url: 'Personal/login',
				data: {
					user_name: user_name,
					user_pwd: user_pwd,
					needIdentify: 1,
					identify: identify,
					sendIdentify: trueCode
				}
			}
			common.loading("登录中...请稍后...");
			common.JsonPost(params, function(data) {
				common.closeLoading();
				console.log(JSON.stringify(data));
				
				//缓存用户信息
				common.setLocVal("userId", data.id);
				common.setLocVal("userName", data.user_name);
				common.setLocVal("realName", data.real_name);
				common.setLocVal("mobile", data.mobile);
				common.setLocVal("avatar", data.avatar);
				common.setLocVal("userData", JSON.stringify(data));
				
				//返回个人中心
				mui.openWindow({
					url: "my.html",
					id: "my"
				})
			})
		});
		
		document.getElementById("register").addEventListener('tap', function() {
			mui.openWindow({
				url: "reg.html",
				id: "reg"
			})
		});
		
		function getCode(){
			var phone = mobilePhone.value;
			if(!common.isTelphone(phone)) {
				common.alert("错误", "请填写正确的手机号！");
				return false;
			}
			
			getCodeBtn.removeEventListener('tap', getCode);
			
			var params = {
				url: "Identify/getEmsCode",
				data: {
					mobile: phone,
					type:2
				}
			};
			common.JsonPost(params, function(data) {
				if(data === false){
					getCodeBtn.addEventListener('tap', getCode);
					return;
				}
				console.log('获取短信验证码' + JSON.stringify(data));
				trueCode = data;
				
				window.elapseTmpInterval = window.setInterval(function() {
					if(window.elapsedCounter === undefined) {
						window.elapsedCounter = 120;
					}
					if(window.elapsedCounter < 1) {
						window.clearInterval(window.elapseTmpInterval);
						window.elapsedCounter = 120;
						getCodeBtn.addEventListener('tap', getCode);
						getCodeBtn.innerHTML = "重新发送";
						return;
					}
					getCodeBtn.innerHTML = "重新发送(" + window.elapsedCounter + "s)";
					window.elapsedCounter--;
				}, 1000)
			})
		}
	</script>

</html>